Android获取各种系统路径的方法
通过Environment获取的
- Environment.getDataDirectory().getPath() : 获得根目录/data 内部存储路径
- Environment.getDownloadCacheDirectory().getPath() : 获得缓存目录/cache
- Environment.getExternalStorageDirectory().getPath(): 获得SD卡目录/mnt/sdcard(获取的是手机外置sd卡的路径)
- Environment.getRootDirectory().getPath() : 获得系统目录/system
通过Context获取的
- Context.getDatabasePath() 返回通过Context.openOrCreateDatabase 创建的数据库文件
Context.getCacheDir().getPath() : 用于获取APP的cache目录/data/data//cache目录
Context.getExternalCacheDir().getPath() : 用于获取APP的在SD卡中的cache目录/mnt/sdcard/android/data//cache
Context.getFilesDir().getPath() : 用于获取APP的files目录 /data/data//files
Context.getObbDir().getPath(): 用于获取APPSDK中的obb目录/mnt/sdcard/Android/obb/
Context.getPackageName() : 用于获取APP的所在包目录
- Context.getPackageCodePath() : 来获得当前应用程序对应的 apk 文件的路径
- Context.getPackageResourcePath() : 获取该程序的安装包路径
完整 操作手机文件 工具类:
public void saveToPhone(View v) {
// FileDir();
SDCardTest();
}
private void FileDir() {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
Log.i("codecraeer", "getFilesDir = " + getFilesDir());
Log.i("codecraeer", "getExternalFilesDir = " + getExternalFilesDir("exter